package com.wxy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wxy.common.R;
import com.wxy.domain.Student;
import com.wxy.domain.Teacher;
import com.wxy.domain.User;
import com.wxy.service.StudentService;
import com.wxy.service.TeacherService;
import com.wxy.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {
    @Autowired
    private StudentService studentService;

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private UserService userService;

    //管理员登录
    @PostMapping("/root/login")
    public R<String>rootLogin(HttpServletRequest request, @RequestBody User user){
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(User::getUsername,user.getUsername());
        if (userService.getOne(queryWrapper) == null)
            return R.error("这不是管理员!");
        if(user.getUsername().equals("root") && user.getPassword().equals(userService.getOne(queryWrapper).getPassword())){
            request.getSession().setAttribute("root",user.getUsername());
            return R.success("登录成功");
        }
        return R.error("业务繁忙");
    }

    /**
     * 修改管理员密码
     * @param request
     * @param user
     * @return
     */
    @PostMapping("/root/update")
    public R<String>rootUpdate(HttpServletRequest request,@RequestBody User user){
        if(request.getSession().getAttribute("root") == null){
            return R.error("当前未登录");
        }
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(User::getUsername,user.getUsername());
        userService.update(user,queryWrapper);
        return R.success("修改成功!");
    }

    //管理员退出
    @PostMapping("/root/logout")
    public R<String> rootLogout(HttpServletRequest request){
        request.getSession().removeAttribute("root");
        return R.success("退出成功");
    }

    //学生登录
    @PostMapping("/student/login")
    public R<String>studentLogin(HttpServletRequest request, @RequestBody Student student){
        LambdaQueryWrapper<Student> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Student::getSusername,student.getSusername());
        Student student1 = studentService.getOne(lambdaQueryWrapper);
        if(student1 == null){
            return R.error("没有该账户噢，点击注册吧!");
        }else {
            if(student1.getSpassword().equals(student.getSpassword())){
                request.getSession().setAttribute("student",student.getSusername());
                return R.success("登录成功!");
            }
            else {
                return R.error("账号或密码错误!");
            }
        }
    }

    //学生退出
    @PostMapping("/student/logout")
    public R<String> StudentLogout(HttpServletRequest request){
        request.getSession().removeAttribute("student");
        return R.success("退出成功");
    }

    /**
     * 获取所有老师的id
     * @return
     */
    @GetMapping("/gettusername")
    public R<List<Long>>getTusername(){
        List<Long>tusername = new ArrayList<>();
        List<Teacher>tlist = teacherService.list();
        for (Teacher teacher: tlist){
            tusername.add(teacher.getTusername());
        }
        return R.success(tusername);
    }

    //学生注册
    @PostMapping("/student/register")
    public R<String>studentRegister(@RequestBody Student student){
         log.info(student.toString());
        LambdaQueryWrapper<Student> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Student::getSusername,student.getSusername());
        Student student1 = studentService.getOne(lambdaQueryWrapper);
        if(student1 == null){
            log.info("开始注册");
            LambdaQueryWrapper<Teacher> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Teacher::getTusername,student.getTusername());
            Teacher teacher = teacherService.getOne(queryWrapper);
            if(teacher == null){
                return R.error("该老师不存在!");
            }
            studentService.save(student);
            return R.success("注册成功");
        }
        return R.error("该账户已经存在!");
    }
    //老师登录
    @PostMapping("/teacher/login")
    public R<String>teacherLogin(HttpServletRequest request, @RequestBody Teacher teacher){
        LambdaQueryWrapper<Teacher> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Teacher::getTusername,teacher.getTusername());
        Teacher teacher1 = teacherService.getOne(lambdaQueryWrapper);
        if(teacher1 == null){
            return R.error("没有该账户噢，点击注册吧!");
        }else {
            if(teacher1.getTpassword().equals(teacher.getTpassword())){
                request.getSession().setAttribute("teacher",teacher.getTusername());
                return R.success("登录成功!");
            }
            else {
                return R.error("账号或密码错误!");
            }
        }
    }

    //老师退出
    @PostMapping("/teacher/logout")
    public R<String> TeacherLogout(HttpServletRequest request){
        request.getSession().removeAttribute("teacher");
        return R.success("退出成功");
    }

    //老师注册
    @PostMapping("/teacher/register")
    public R<String>teacherRegister(@RequestBody Teacher teacher){
        log.info(teacher.toString());
        LambdaQueryWrapper<Teacher> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Teacher::getTusername,teacher.getTusername());
        Teacher teacher1 = teacherService.getOne(lambdaQueryWrapper);
        if(teacher1 == null){
            log.info("开始注册");
            LambdaQueryWrapper<Teacher> queryWrapper = new LambdaQueryWrapper<>();
            teacherService.save(teacher);
            return R.success("注册成功");
        }
        return R.error("该账户已经存在!");
    }

    //管理员的CRUD

    /**
     * 分别是获取老师和学生的所有数据
     * @param request
     * @return
     */
    @GetMapping("/root/teacher")
    public R<List<Teacher>>getAllTch(HttpServletRequest request){
        if(request.getSession().getAttribute("root") == null){
            return R.error("您当前没有这个权限");
        }
        return R.success(teacherService.list());

    }
    @GetMapping("/root/student")
    public R<List<Student>>getAllStu(HttpServletRequest request){
        if(request.getSession().getAttribute("root") == null){
            return R.error("您当前没有这个权限");
        }
        return R.success(studentService.list());
    }
    @DeleteMapping("/root/student/{susername}")
    public R<String>deleteStu(@PathVariable Long susername){
        boolean b = teacherService.removeById(susername);
        return R.success("删除成功");
    }
    @DeleteMapping("/root/teacher/{tusername}")
    public R<String>deleteTch(@PathVariable Long tusername){
        boolean b = teacherService.removeById(tusername);
        return R.success("删除成功");
    }

    /**
     * 管理员修改老师和学生数据
     * @param teacher
     * @return
     */
    @PutMapping("/root/teacher")
    public R<String>updateTch(@RequestBody Teacher teacher){
        LambdaQueryWrapper<Teacher>queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Teacher::getTusername,teacher.getTusername());
        teacherService.update(teacher,queryWrapper);
        return R.success("修改成功!");
    }
    @PutMapping("/root/student")
    public R<String>updateStu(@RequestBody Student student){
        LambdaQueryWrapper<Student>queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Student::getSusername,student.getSusername());
        studentService.update(student,queryWrapper);
        return R.success("修改成功!");
    }

    /**
     * 老师修改密码
     * @param teacher
     * @return
     */
    @PostMapping("/teacher/repass")
    public R<String> tchChangePassword(@RequestBody Teacher teacher){
        LambdaQueryWrapper<Teacher> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(Teacher::getTusername,teacher.getTusername());
        teacherService.update(teacher,queryWrapper);
        return R.success("修改成功!");
    }

    /**
     * 修改学生密码
     * @param student
     * @return
     */
    @PostMapping("/student/repass")
    public R<String> stuChangePassword(@RequestBody Student student){
        LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(Student::getSusername,student.getSusername());
        studentService.update(student,queryWrapper);
        return R.success("修改成功!");
    }

}
